package com.csair.common.helper;

import android.content.Context;
import android.support.annotation.StringRes;
import android.text.TextUtils;

import com.csair.common.AppUtilsConfig;
import com.csair.common.CommonApplication;
import com.csair.common.utils.SharedPreferencesUtil;
import com.csair.mbp.common.R;



public class Constants {

    private static String COMMON_BASE, TOUCH_BASE;

    /**
     * 连后台地址
     */
    public static String commonUrl(@StringRes int url, Object... params) {
        Context context = CommonApplication.getApplication();
        return getCommonBase() + context.getString(url, params);
    }

    /**
     * 设置后台基地址
     */
    public static String getCommonBase() {
        if (!TextUtils.isEmpty(COMMON_BASE)) return COMMON_BASE;
        if (!TextUtils.isEmpty(COMMON_BASE = getLastCommonBase())) return COMMON_BASE;
        Context context = CommonApplication.getApplication();
        return COMMON_BASE = context.getString(R.string.common_URL_COMMON);

    }

    /**
     * 连触屏地址
     */
    public static String touchUrl(@StringRes int urlResId, Object... params) {
        Context context = CommonApplication.getApplication();
        String url = getTouchBase() + context.getString(urlResId, params);
        if (!url.contains("?")) return url + "?t=fromClient";
        String[] subUrl = url.split("\\?");
        return subUrl[0] + "?t=fromClient&" + subUrl[1];

    }

    /**
     * 连触屏地址
     */
    public static String touchUrl(String url) {
        url = getTouchBase() + url;
        if (!url.contains("?")) return url + "?t=fromClient";
        if (url.contains("t=fromClient")) return url;
        String[] subUrl = url.split("\\?");
        return subUrl[0] + "?t=fromClient&" + subUrl[1];
    }

    /**
     * 设置触屏基地址
     */
    public static String getTouchBase() {
        if (!TextUtils.isEmpty(TOUCH_BASE)) return TOUCH_BASE;
        if (!TextUtils.isEmpty(TOUCH_BASE = getLastTouchBase())) return TOUCH_BASE;
        Context context = CommonApplication.getApplication();
        return TOUCH_BASE = context.getString(R.string.common_URL_TOUCH);
    }

    /**
     * 连其他地址
     */
    public static String otherUrl(@StringRes int urlResId, @StringRes int testUrlResId) {
        Context context = CommonApplication.getApplication();
        return context.getString(isLastOtherUrlInReleaseEnvironment() ? urlResId : testUrlResId);
    }

    /**
     * 更新该类中后台的基地址
     */
    public static void refreshCommonBase(String newUrlBase) {
        setLastCommonBase(COMMON_BASE = newUrlBase);
    }

    /**
     * 更新该类中触屏的基地址
     */
    public static void refreshTouchBase(String newTouchBase) {
        setLastTouchBase(TOUCH_BASE = newTouchBase);
    }

    /**
     * 更新其他地址的状态是属于测试环境还是生产环境
     */
    public static void refreshOtherUrlEnvironment(boolean isRelease) {
        setIsLastOtherUrlInReleaseEnvironment(isRelease);
    }

    private static String getLastCommonBase() {
        return SharedPreferencesUtil.getInstance(AppUtilsConfig.PREFERENCE_APPCONFIG_FILENAME).getString(AppUtilsConfig.DOMAIN_REQUEST_URL_KEY);
    }

    private static void setLastCommonBase(String url) {
        SharedPreferencesUtil.getInstance(AppUtilsConfig.PREFERENCE_APPCONFIG_FILENAME).saveString(AppUtilsConfig.DOMAIN_REQUEST_URL_KEY,url);
    }

    private static String getLastTouchBase() {
        return SharedPreferencesUtil.getInstance(AppUtilsConfig.PREFERENCE_APPCONFIG_FILENAME).getString(AppUtilsConfig.DOMAIN_TOUCH_URL_KEY);
    }

    private static void setLastTouchBase(String url) {
        SharedPreferencesUtil.getInstance(AppUtilsConfig.PREFERENCE_APPCONFIG_FILENAME).saveString(AppUtilsConfig.DOMAIN_TOUCH_URL_KEY,url);
    }

    public static boolean isLastOtherUrlInReleaseEnvironment() {
        return SharedPreferencesUtil.getInstance(AppUtilsConfig.PREFERENCE_APPCONFIG_FILENAME).getBoolean(AppUtilsConfig.URL_IS_RELEASE_ENVIRONMENT,true);
    }

    private static void setIsLastOtherUrlInReleaseEnvironment(boolean isRelease) {
        SharedPreferencesUtil.getInstance(AppUtilsConfig.PREFERENCE_APPCONFIG_FILENAME).saveBoolean(AppUtilsConfig.URL_IS_RELEASE_ENVIRONMENT,isRelease);
    }
}
